import 'package:dio/dio.dart';
import 'package:mall/core/models/access_token.dart';
import 'package:mall/core/services/auth.dart';
import 'package:mall/core/providers/view_model.dart';

class LoginModel extends ViewModel {
  final Auth auth;
  String _errorMessage;
  AccessToken _accessToken;

  LoginModel(this.auth) : super();

  String get errorMessage => _errorMessage;
  AccessToken get accessToken => _accessToken;

  /// 登录
  Future<ViewState> oauthToken(String username, String password) async {
    viewState = ViewState.busy;
    try {
      _accessToken = await auth.oauthToken(username, password);
      viewState = ViewState.success;
    } catch (e) {
      _errorMessage = e is DioError ? e.message : e.toString();
      viewState = ViewState.error;
    }
    return viewState;
  }
}
